Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
How to import methods from external file that also use "this" in a Vue application

I have a vue file that has quite a few methods that I am looking to export into a separate file. The problem is some of these methods make use of the this keyword, and I can't figure out how to import these methods properly. Here is a minimal example

App.vue

export default {
  ...,
  data () {
    return { // assume these get generated properly through the app
        this.orderAmount = null,
        this.tax = null,
        this.totalAmount = null
        
    }
  },
  methods: {
    assignForAccount () {
      this.totalAmount = this.tax * this.totalAmount
      }
    }
}

I would like to move methods such as assignForAccount to a separate file called methods.js, but it I am getting undefined errors when I try to do this:

App.vue

import { assignForAccount } from './methods.js'

export default {
  ...,
  data () {
    return { // assume these get generated properly through the app
        this.orderAmount = null,
        this.tax = null,
        this.totalAmount = null

    }
  },
  methods: {
       assignForAccount
    }
}

methods.js

export const assignForAccount = (context) => {
  context.totalAmount = context.tax * context.orderAmount
}

TIA

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to pass both this.tax and this.totalAmount from App.vue file into method.js file for the calculations.

method.js :

export function assignForAccount = (tax, totalAmount) => {
    return tax * totalAmount
}

App.vue :

import { assignForAccount } from './methods.js'

methods: {
    this.totalAmount = assignForAccount(this.tax, this.totalAmount);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda